package com.zcah.system.controller;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcah.common.vo.R;
import com.zcah.system.dao.JwtuserDao;
import com.zcah.system.domain.JwtuserDO;
import com.zcah.system.util.JWTUtils;

@RestController
public class LoginController {

	@Autowired
	private JwtuserDao userDao;

	@PostMapping("/login")
	public R login(@RequestParam("username") String username, @RequestParam("password") String password) {
		JwtuserDO user = userDao.getUser(username);
		if (user != null && user.getPassword().equals(password)) {
			R ok = R.ok("登陆成功");
			ok.put("token", JWTUtils.sign(username, password));
			return ok;
		} else {
			throw new UnauthorizedException("用户名或密码错误！");
		}
	}
}
